package tk.mybatis.springboot.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.springboot.async.Task1;
import tk.mybatis.springboot.client.TestClient;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/test")
public class TestController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	// 注入RedisTemplate对象
	@Resource(name = "redisTemplate")
	private RedisTemplate<String, String> template;

	@Autowired
	private Task1 task1;

	@Autowired
	private TestClient testClient;

	@Value(value = "name")
	private String name;

	@RequestMapping
	public int error() {
		return 100 / 0;
	}

	// 测试异步task1.
	@RequestMapping("/task1")
	public String task1() throws Exception {
		task1.doTaskOne();
		task1.doTaskTwo();
		task1.doTaskThree();
		return "task1";
	}

	@RequestMapping("/name")
	public String name() {
		logger.info("日志输出测试 Info");
		return name;
	}

	@RequestMapping("/redis")
	public String redis() {
		// 具体调用
		Map<String, String> map = new HashMap<String, String>();
		map.put("value", "code");
		map.put("key", "keyValue");
		template.opsForHash().putAll("hashOps", map);
		return "success";
	}

}
